package com.base.cn.platform.os.controller.user.account;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.user.AccountStatus;
import com.base.cn.platform.os.entity.user.account.CusUserAccount;
import com.base.cn.platform.os.entity.user.user.UserCondition;
import com.base.cn.platform.os.service.user.account.CusUserAccountBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @Data 等级
 * @ClassName   CusUserAccountController
 * @author :ll
 * @Create Date : 2018年4月25日
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserAccountController extends BaseController {
    @Autowired
    private CusUserAccountBiz cusUserAccountBiz;

    /**
     * 分页搜索账户列表
     *
     * @param currentPage 分页
     * @return
     */
    @RequestMapping("/queryCusUserAccountListPage")
    public PageInfo<CusUserAccount> queryCusUserAccountListPage(@RequestBody UserCondition userContainColumns,
                                                                @RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage) {
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(9);
        return cusUserAccountBiz.queryAllUserAccountListPage(userContainColumns, page);
    }

    /**
     * 操作账户金额
     * type=1银行充值，type=2银行冻结金额,type=3银行解冻金额
     * type=4充值卡入账,type=5充值卡冻结金额,type=6充值卡解冻金额
     * type=7银行扣款，type=8充值卡扣款
     * @param id 账户ID
     * @param type 操作类型
     * @param amount 操作金额
     * @return Map<String , Object>
     */

    @RequestMapping("/updateUserAccountAmount")
    public Map<String, Object> updateUserAccountAmount(@RequestParam("createUserId")BigDecimal createUserId,
                                                       @RequestParam("id")BigDecimal id,
                                                       @RequestParam("type") int type,
                                                       @RequestParam("amount") double amount){
        CusUserAccount userAccount = cusUserAccountBiz.findById(id);
        if (userAccount.getAccountStatus().equals(AccountStatus.FROZEN.toString())) {

            return ResultUtil.ERROR("该账户已被冻结，禁止操作");
        }
        if (userAccount == null) {
            return ResultUtil.ERROR("数据错误操作失败");
        }
        if (amount <= 0) {
            return ResultUtil.ERROR("操作金额不能小于0");
        }
        BigDecimal bigAmount = new BigDecimal(amount + "");//操作的金额
        //账户总余额
        BigDecimal balance = userAccount.getBalance();
        BigDecimal cashAmount = userAccount.getCashAmount();//银行当可用金额
        BigDecimal vmAmount = userAccount.getVmAmount();//当前充值卡金额
        //后台暂时不提供直接操作银行卡充值,注释type==1的条件
        if (/*type == 1 ||*/ type == 2 || type == 3) {
            BigDecimal cashForzenAmount = userAccount.getCashForzenAmount();//银行当前冻结金额
            if (type == 1) {//type=1银行充值
                cashAmount = cashAmount.add(bigAmount);
                balance = balance.add(bigAmount);
                if (balance.doubleValue() >= 100000000.0 || cashAmount.doubleValue() >= 100000000.0) {
                    return ResultUtil.ERROR("账户总金额不能超过99999999");
                }
            } else if (type == 2) {//type=2银行冻结金额
                if (cashAmount.compareTo(bigAmount) == -1) {//可操作金额小于操作金额
                    return ResultUtil.ERROR("银行金额小于冻结的金额");
                }
                cashAmount = cashAmount.subtract(bigAmount);
                cashForzenAmount = cashForzenAmount.add(bigAmount);
                balance = balance.subtract(bigAmount);
            } else if (type == 3) {//type=3银行解冻金额
                if (bigAmount.compareTo(cashForzenAmount) == 1) {//如果操作金额大于可操作金额
                    return ResultUtil.ERROR("银行解冻金额大于冻结金额");
                }
                cashAmount = cashAmount.add(bigAmount);
                cashForzenAmount = cashForzenAmount.subtract(bigAmount);
                balance = balance.add(bigAmount);
            }
            userAccount.setCashForzenAmount(cashForzenAmount);
        } else if (type == 4 || type == 5 || type == 6) {
            BigDecimal vmForzenAmount = userAccount.getVmForzenAmount();//当前充值卡冻结金额
            if (type == 4) {//type=4充值卡入账
                vmAmount = vmAmount.add(bigAmount);
                balance = balance.add(bigAmount);
                if (balance.doubleValue() >= 100000000.00 || vmAmount.doubleValue() >= 100000000.00) {
                    return ResultUtil.ERROR("账户总金额不能超过99999999");
                }
            } else if (type == 5) {//type=5充值卡冻结金额
                if (vmAmount.compareTo(bigAmount) == -1) {//可操作金额小于操作金额
                    return ResultUtil.ERROR("充值卡金额小于冻结的金额");
                }
                vmAmount = vmAmount.subtract(bigAmount);
                vmForzenAmount = vmForzenAmount.add(bigAmount);
                balance = balance.subtract(bigAmount);
            } else if (type == 6) {//type=6充值卡解冻金额
                if (bigAmount.compareTo(vmForzenAmount) == 1) {
                    return ResultUtil.ERROR("充值卡解冻金额大于冻结金额");
                }
                vmAmount = vmAmount.add(bigAmount);
                vmForzenAmount = vmForzenAmount.subtract(bigAmount);
                balance = balance.add(bigAmount);
            }
            userAccount.setVmForzenAmount(vmForzenAmount);
        } else if (type == 7 || type == 8) {//type=7银行卡扣款，type=8充值卡扣款
            if (type == 7) {
                if (bigAmount.compareTo(cashAmount) == 1) {
                    return ResultUtil.ERROR("银行扣款金额大于现有金额");
                }
                cashAmount = cashAmount.subtract(bigAmount);
                balance = balance.subtract(bigAmount);
            } else if (type == 8) {
                if (bigAmount.compareTo(vmAmount) == 1) {
                    return ResultUtil.ERROR("充值卡扣款金额大于现有金额");
                }
                vmAmount = vmAmount.subtract(bigAmount);
                balance = balance.subtract(bigAmount);
            }
        }else if (type == 11) {
                //积分兑换
                cashAmount = cashAmount.add(bigAmount);
                balance = balance.add(bigAmount);
                if (balance.doubleValue() >= 100000000.0 || cashAmount.doubleValue() >= 100000000.0) {
                    return ResultUtil.ERROR("账户总金额不能超过99999999");
                }
        }
        userAccount.setBalance(balance);
        userAccount.setCashAmount(cashAmount);
        userAccount.setVmAmount(vmAmount);
        cusUserAccountBiz.tx_updateUserAccountAmount(userAccount,createUserId,type,amount,null);
        return ResultUtil.SUCCESS("操作成功",userAccount);
    }

    /**
     * 修改账户状态
     * @param status  状态，ACTIVE正常，FROZEN冻结
     * @param id 账户 ID
     * @return Map<String , Object>
     */
    @RequestMapping("/updateUserAccountStatus")
    public Map<String,Object> updateUserAccountStatus(@RequestParam("status") String status,
                                                     @RequestParam("id") BigDecimal id){
            CusUserAccount userAccount = cusUserAccountBiz.findById(id);
            if(userAccount==null){
                return ResultUtil.ERROR("数据错误，操作失败") ;
            }
            if(!status.equals(AccountStatus.ACTIVE.toString()) && !status.equals(AccountStatus.FROZEN.toString())){
                return ResultUtil.ERROR("参数错误，操作失败");
            }
            userAccount.setAccountStatus(status);
            String message = "";
            if(status.equals(AccountStatus.ACTIVE.toString())){
                message="恢复用户账户成功";
            }else if(status.equals(AccountStatus.FROZEN.toString())){
                message="冻结用户账户成功";
            }
            cusUserAccountBiz.updateById(userAccount,"id");
            return ResultUtil.SUCCESS(message);
    }

    /**
     * 查看账户详情
     * @param userId
     * @return 显示页面
     */
    @RequestMapping("/userAccountInfo")
    public CusUserAccount userAccountInfo(@RequestParam("userId")BigDecimal userId,@RequestParam("userData") boolean userData){
        return  cusUserAccountBiz.queryUserAccountByUserId(userId,userData);
    }

    /**
     * 账户入账,充值 入款，出账.消费
     *
     * @param userId             学员用户id
     * @param trxAmount          　交易金额
     * @param accountType        　账户操作类型，cash or vm
     * @param accountHistoryType 　账户历史记录类型
     * @param orderId            　订单号,订单表主键id
     * @param orderNo            　订单号,对系统的的
     * @param outTradeNo         　提交到支付宝的,(充值卡是用卡号)
     * @param isDisplay          　在用户我的账户历史中是否显示 0显示 1不显示
     * @param accountBizType     　交易类型，课程、会员、图书等
     * @param createUserId          后台管理ID
     * @param status             1.出账 2.进账
     */
    @RequestMapping("/updateAccountMoney")
    public void updateAccountMoney(@RequestParam("userId") BigDecimal userId,
                                   @RequestParam(value = "trxAmount") double trxAmount,
                                   @RequestParam("accountType") String accountType,
                                   @RequestParam("accountHistoryType") String accountHistoryType,
                                   @RequestParam("orderId") BigDecimal orderId,
                                   @RequestParam(value = "orderNo",required = false) String orderNo,
                                   @RequestParam(value = "outTradeNo",required = false) String outTradeNo,
                                   @RequestParam("isDisplay") boolean isDisplay,
                                   @RequestParam("accountBizType") String accountBizType,
                                   @RequestParam("createUserId") BigDecimal createUserId,
                                   @RequestParam("status") int status) throws Exception {
        cusUserAccountBiz.updateAccountMoney(userId,trxAmount,accountType,accountHistoryType,orderId,orderNo,
                outTradeNo,isDisplay,accountBizType,createUserId,status,false);
    }

}